package com.sangfor.ssl.common;

import com.sangfor.bugreport.logger.Log;
import java.util.Timer;
import java.util.TimerTask;

/* loaded from: classes2.dex */
public class ScheduleTimer {
    public static final int CANCLE = 3;
    public static final int INIT = 0;
    public static final int PAUSE = 2;
    public static final int RUNNING = 1;
    private static final String TAG = "RecordTimer";
    private long mDelay;
    private long mPeriod;
    private ScheduleTimerTask mScheduleTimerTask;
    private Timer mTimer;
    boolean mTimerActived;
    private int mTimerState;
    private TimerTask mTimerTask;
    private long mTimerRunTime = 0;
    private long mActiveTime = 0;

    /* loaded from: classes2.dex */
    public interface ScheduleTimerTask {
        void run();
    }

    public ScheduleTimer() {
        setTimerActiveTime(0L);
        this.mTimerState = 0;
    }

    public void start(ScheduleTimerTask scheduleTimerTask, long j, long j2) {
        if (this.mTimerState != 0) {
            Log.error(TAG, "can not start an timer when it at state : " + this.mTimerState);
            return;
        }
        this.mScheduleTimerTask = scheduleTimerTask;
        start(j, j2);
    }

    private void start(long j, long j2) {
        cancleTimerAndTask();
        startTimerTask(j, j2);
        this.mTimerState = 1;
    }

    public void resume() {
        if (this.mTimerState != 2) {
            Log.info(TAG, "can not resume an timer when it at state : " + this.mTimerState);
            return;
        }
        startTimerTask(getTimerResumeDelay(), this.mPeriod);
        this.mTimerState = 1;
    }

    public void pause() {
        if (this.mTimerState != 1) {
            Log.error(TAG, "can not pause an timer when it at state : " + this.mTimerState);
            return;
        }
        cancleTimerAndTask();
        updateTimerRunTime();
        this.mTimerState = 2;
    }

    public void cancel() {
        cancleTimerAndTask();
        setTimerActiveTime(0L);
        this.mTimerState = 0;
    }

    public void changeTimerIntervalTime(long j, long j2) {
        if (this.mTimerState == 0) {
            Log.debug(TAG, "must start timer at first");
            return;
        }
        cancel();
        start(j, j2);
    }

    public int getState() {
        return this.mTimerState;
    }

    private void cancleTimerAndTask() {
        TimerTask timerTask = this.mTimerTask;
        if (timerTask != null) {
            timerTask.cancel();
            this.mTimerTask = null;
        }
        Timer timer = this.mTimer;
        if (timer != null) {
            timer.purge();
            this.mTimer.cancel();
            this.mTimer = null;
        }
    }

    private void startTimerTask(long j, long j2) {
        this.mDelay = j;
        this.mPeriod = j2;
        setTimerActiveTime(System.currentTimeMillis());
        this.mTimer = new Timer(true);
        ExecTimerTask execTimerTask = new ExecTimerTask();
        this.mTimerTask = execTimerTask;
        if (j >= 0 && j2 > 0) {
            this.mTimer.scheduleAtFixedRate(execTimerTask, j, j2);
            Log.info(TAG, "set interval time : " + j + "|" + j2);
            return;
        }
        Log.error(TAG, "illegal interval time : " + j + "|" + j2);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public synchronized void setTimerActiveTime(long j) {
        if (j > 0) {
            this.mActiveTime = j;
        } else {
            this.mActiveTime = 0L;
            this.mTimerRunTime = 0L;
        }
    }

    private synchronized void updateTimerRunTime() {
        this.mTimerRunTime = System.currentTimeMillis() - this.mActiveTime;
    }

    private synchronized long getTimerResumeDelay() {
        long j;
        j = (this.mTimerActived ? this.mPeriod : this.mDelay) - this.mTimerRunTime;
        if (j < 0) {
            j = 0;
        }
        return j;
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes2.dex */
    public class ExecTimerTask extends TimerTask {
        private ExecTimerTask() {
        }

        @Override // java.util.TimerTask, java.lang.Runnable
        public void run() {
            ScheduleTimer.this.mScheduleTimerTask.run();
            ScheduleTimer.this.setTimerActiveTime(System.currentTimeMillis());
            ScheduleTimer.this.mTimerActived = true;
        }
    }
}
